<?php
/**
 * Created by PhpStorm.
 * User: Dave
 * Date: 2018/4/27
 * Time: 10:21
 */

namespace Biz\validate;

use function Amp\all;
use Biz\model\ShopCarouselBanner as mShopCarouselBanner;
use Biz\model\ShopCarouselBanner;

class ShopBanner extends \Doraemon\pockets\validate\Base
{
    public static function rules()
    {
        return [

            ['title', 'callback', ['callback' => [new self(), 'checkTitle']], '标题输入有误，最多不超过4个汉字', ['create', 'update']],

            ['banner_desc', 'callback', ['callback' => [new self(), 'checkBannerDesc']], '描述 输入有误，请重新输入', ['create', 'update']],

            ['start_time', 'callback',['callback' => [new self(), 'checkTime']], '请选择开始日期', ['create', 'update']],
            ['end_time', 'callback', ['callback' => [new self(), 'checkTime']], '请选择结束日期', ['create', 'update']],

            ['link_type', 'callback', ['callback' => [new self(), 'checkLinkAndType']], '请选择链接类型,订单确认页不支持链接', ['create', 'update']],

            ['link', 'callback', ['callback' => [new self(), 'checkLinkAndType']], '请填写链接,订单确认页不支持链接', ['create', 'update']],


            ['sort', 'required', null, '请填写权重值', ['create', 'update']],
            ['sort','regex',['pattern'=>'/^[\0-9]+$/u'],'权重只能输入数字',['create','update']],
            ['sort', 'callback', ['callback' => [new self(), 'checkWeight']], '当前权重值已存在', ['create', 'update']],

        ];
    }

    public function checkLinkAndType($value, $valueIdentifier, $allData)
    {
        if($allData['type'] == mShopCarouselBanner::TYPE_ORDER_CONFIRM) {
            if ($allData['show_type'] == 12 && ($allData['link_type'] != 100 || $allData['link'])) {
                return false;
            } elseif ($allData['link_type'] != 100 && empty($allData['link'])) {
                return false;
            }
        }
        return true;
    }

    public function checkWeight($value, $valueIdentifier, $allData)
    {
        $where = [];
        if (!empty($allData['bannerid'])){
            $where['AND']['id[!]'] = $allData['bannerid'];
        }
        $where['AND']['show_type'] = $allData['show_type'];


        $weight = mShopCarouselBanner::checkWeight($allData['sort'], $where);
        if ($weight){

            return false;
        }

        return true;

    }

    public function checkTime($value, $valueIdentifier, $allData)
	{
		if ($allData['type'] != ShopCarouselBanner::TYPE_FIVE_POSITION && empty($value)) {
			return false;
		}
		return true;
	}
    public function checkTitle($value, $valueIdentifier, $allData)
    {
        if(empty($value)){
        	if ($allData['type'] == ShopCarouselBanner::TYPE_FIVE_POSITION) {
				return false;
			}
            return true;
        }

        if(mb_strlen($value) > 4){
            return false;
        }

        return true;
    }

    public function checkBannerDesc($value, $valueIdentifier, $allData)
    {
        if(empty($value)){
            return true;
        }

        $notChinese = 0;
        // 将输入的信息转换为数组。 查看输入的是否全部为汉字
        preg_match_all("/./u", $value, $arr);
        foreach (current($arr) as $key => $val) {
            if(strlen($val) == 1) {
                $notChinese = 1;
                break;
            }
        }


        if ($allData['type'] == mShopCarouselBanner::TYPE_FOUR) {
            $data = explode('^', $value);
            foreach($data as $value){
                if(mb_strlen($value) > 6){
                    return false;
                }
            }
        } elseif($allData['type'] == mShopCarouselBanner::TYPE_THREE_TITLE) {
            if($notChinese) {
                if(mb_strlen($value) > 14){
                    return false;
                }
            } else {
                if(mb_strlen($value) > 7){
                    return false;
                }
            }
        }


        return true;
    }
}